package com.itheima.util;

import com.auth0.jwt.interfaces.DecodedJWT;
import lombok.extern.log4j.Log4j2;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import javax.servlet.http.HttpServletRequest;

/**
 * @Author longbosheng
 * @Date 2022/6/14 14:22
 * @PackageName:com.itheima.util
 * @ClassName: RequestParesUtil
 * @Description: TODO
 * @Version 1.0
 */
@Log4j2
@Component

public class RequestParesUtil {
    /**
     * 从token获取员工的id
     * @param request
     * @return
     */
    public static String RequestPares(HttpServletRequest request){
        String token = request.getHeader("Authorization");
        DecodedJWT verify = JwtUtil.verify(token);
        String id = verify.getClaim("id").asString();
        log.info("获取到请求头id:"+id);
        System.out.println("获取到请求头id:"+id);
        if (StringUtils.hasText(id)){
            return id;
        }else {
            return null;
        }
    }

    /**
     * 从token获取用户的id
     * @param request
     * @return
     */
    public static String getRequestId(HttpServletRequest request){
        String token = request.getHeader("AuthorizationUser");
        DecodedJWT verify = JwtUtil.verify(token);
        String id = verify.getClaim("id").asString();
        log.info("获取到请求头id:"+id);
        System.out.println("获取到请求头id:"+id);
        if (StringUtils.hasText(id)){
            return id;
        }else {
            return null;
        }
    }
}
